import {AfterViewInit, Component, ElementRef, OnInit, Renderer2} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {SsoService} from '../service/sso.service';
import {environment} from '../../../environments/environment';
import {ReturnForm} from '@sb/base';
import {LoginUser} from '../entity/login-user';
import {SessionUser} from '../entity/session-user';
import {ShowMenu, ShowMenus} from '../entity/show-menu';
import {MenuService} from '../service/menu.service';
import {LoginGuard} from '../service/login.guard';
import {OuOrgUserService} from '../../system/ou/ou-org-user/service/ou-org-user.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
    selector: 'sb-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit, AfterViewInit {

    /**
     * 错误消息
     */
    errorMessage = '';

    /**
     * 是否是发布模式
     */
    production = environment.production;

    /**
     * 验证码url
     */
    verifyCodeSrc;

    /**
     * 标题地址
     */
    titleImageSrc;

    /**
     * 登录对象
     */
    login: LoginUser = {
        userName: '',
        password: '',
        orgId: '',
        verifyCode: null
    };

    _loginFor: string;

    systemLogin: boolean;

    resetKey = '';
    resetPassword = '';
    resetText = '当前密码已过期，';

    validateForm: FormGroup;

    resetPasswordForm: FormGroup;

    constructor(private fb: FormBuilder,
                public ssoService: SsoService,
                private activatedRoute: ActivatedRoute,
                private router: Router,
                private el: ElementRef,
                private renderer2: Renderer2,
                private menuService: MenuService,
                private orgUserService: OuOrgUserService,
                private message: NzMessageService,
    ) {
    }

    get adminQueryParam() {
        if (this._loginFor) {
            return {system: true, loginFor: this._loginFor};
        } else {
            return {system: true};
        }
    }

    get normalQueryParam() {
        if (this._loginFor) {
            return {loginFor: this._loginFor};
        } else {
            return {};
        }
    }

    submitForm(): void {
        for (const i in this.validateForm.controls) {
            this.validateForm.controls[i].markAsDirty();
            this.validateForm.controls[i].updateValueAndValidity();
        }
    }

    ngOnInit(): void {
        this.titleImageSrc = `${environment.baseServerUrl}main/titleImage`;
        //
        // timer(1000).subscribe(v => {
        //     this.title = this.titleService.title;
        // });

        // 检查是否在登录状态，进行跳转
        this.ssoService.checkLogin().subscribe(v => {
            if (v.success) {
                this.doRedirect();
            }
        });

        this.activatedRoute.queryParams.subscribe((params: Params) => {
            const loginFor: string = params['loginFor'];
            if (!loginFor) {
                this._loginFor = null;
            } else {
                this._loginFor = decodeURIComponent(loginFor);
            }

            this.systemLogin = params['system'] === 'true';

            const o = {
                password: [null, [Validators.required]],
                rememberMe: [null]
            };

            if (this.production) {
                o['verifyCode'] = [null, [Validators.pattern(/^[0-9]{4}$/)]];
            }

            if (!this.systemLogin) {
                o['userName'] = [null, [Validators.required]];
            }

            this.validateForm = this.fb.group(o);

            this.validateForm.statusChanges.subscribe((value) => {
                this.errorMessage = null;
            });

            this.resetPasswordForm = this.fb.group({
                password: [null, [Validators.required]],
            });
        });

        this.refreshVerifyCode();

        this.ssoService.reloadPublicKey();

    }

    ngAfterViewInit(): void {

    }

    doLogin() {
        if (this.validateForm.valid) {
            this.ssoService.login(this.login, this.systemLogin).subscribe((returnForm: ReturnForm<SessionUser>) => {
                if (returnForm.success) {
                    this.doRedirect();
                } else {
                    if (returnForm.status === -6) {
                        const temp = returnForm.errorMessage.split('~');
                        this.resetKey = temp[0];
                        this.resetText = temp[1];
                        this.resetPassword = '';
                    } else {
                        this.resetKey = '';
                        this.errorMessage = returnForm.errorMessage;
                    }
                    this.refreshVerifyCode();
                }
            });
        }
    }

    doResetPassword() {
        if (this.resetPassword && this.resetKey) {
            this.ssoService.resetPasswordByKey(this.resetKey, this.resetPassword).subscribe((returnForm: ReturnForm<SessionUser>) => {
                if (returnForm.success) {
                    this.resetKey = null;
                    this.refreshVerifyCode();
                } else {
                    this.resetText = returnForm.errorMessage;
                }
            });
        } else {
            this.message.info('请输入密码');
        }
    }

    refreshVerifyCode() {
        this.verifyCodeSrc = `${environment.baseServerUrl}sso/getVerifyCodeImage?_=${new Date().getTime()}`;
    }

    forgotPassword() {
        this.message.info('请联系管理员重置密码！');
    }

    private doRedirect() {
        if (this._loginFor && !this._loginFor.startsWith('/login')) {
            const index = this._loginFor.indexOf('?');
            const url = this._loginFor.substr(0, index);
            const queryParam = {};
            if (index >= 0) {
                const o = this._loginFor.substr(index + 1);
                for (const a of o.split('&')) {
                    const temp = a.split('=');
                    queryParam[temp[0]] = temp[1];
                }
            }
            if (this._loginFor.lastIndexOf('/') === 0) {
                this.menuService.getShowMenu().subscribe((returnForm: ReturnForm<ShowMenus>) => {
                    if (returnForm.success) {
                        const showMenus = returnForm.message;
                        const menu = this.findMenu(showMenus);
                        if (menu !== null) {
                            let exist = false;
                            for (let i = 0; i < this.router.config.length; i++) {
                                const c = this.router.config[i];
                                exist = c.path === menu.code;
                            }
                            if (!exist) {
                                if (menu.urlType === 'ABSOLUTE') {
                                    this.router.config.splice(1, 0, {
                                        path: menu.code,
                                        loadChildren: 'src/app/main/redirect/redirect.module#RedirectModule',
                                        data: {breadcrumb: menu.name, link: menu.loadChildren},
                                        canLoad: [LoginGuard],
                                    });
                                } else {
                                    this.router.config.splice(1, 0, {
                                        path: menu.code,
                                        loadChildren: menu.loadChildren,
                                        data: {breadcrumb: menu.name},
                                        canLoad: [LoginGuard],
                                    });
                                }
                            }
                            this.router.navigate([url], {
                                queryParams: queryParam
                            });
                        } else {
                            this.router.navigate(['/']);
                        }
                    }
                });
            } else {
                this.router.navigate([url], {
                    queryParams: queryParam
                });
            }
        } else {
            this.router.navigate(['/']);
        }
    }

    private findMenu(menus: ShowMenus): ShowMenu {
        for (let i = 0; i < menus.length; i++) {
            const menu = menus[i];
            if (('/' + menu.code) === this._loginFor) {
                return menu;
            }

            if (menu.children && menu.children.length > 0) {
                const childMenu = this.findMenu(menu.children);
                if (childMenu != null) {
                    return childMenu;
                }
            }
        }
        return null;
    }
}
